package com.kylin.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.paginate.DisplayPage;

public class JspHelper {

	/**
	 * 产生分页界面的翻页工具条
	 * @param dataPage  分页对象
	 * @param actionUrl 产生分页的 Url 
	 * @return
	 */
	public static String getPageBar(DisplayPage dataPage, String actionUrl) {
		try {
			if(dataPage == null)
				return "";
			actionUrl += "&searchId="+dataPage.getSearchId();
			int allPage = dataPage.getFullListSize() / dataPage.getObjectsPerPage();
			if (dataPage.getFullListSize() % dataPage.getObjectsPerPage() > 0)
				allPage++;
			StringBuffer pageBar = new StringBuffer("<table class='pageBar'>");
			pageBar.append("\n  <tr><td class='pageBarTd1'>");
			pageBar.append("\n   总共 ").append(dataPage.getFullListSize()).append(" 条记录 ");
			pageBar.append("共 ").append(allPage);
			pageBar.append(" 页，当前是第 ").append(dataPage.getPageNumber()).append(" 页");
			pageBar.append("\n   </td><td class='pageBarTd2'>");
			if (dataPage.getPageNumber() > 1) {
				pageBar.append("&nbsp;<a href='").append(actionUrl);
				pageBar.append("&page=").append(dataPage.getPageNumber() - 1).append("'>");
				pageBar.append("上一页</a>");
			}
			int block = 5;
			int end = dataPage.getPageNumber() + block;
			if (end > allPage)
				end = allPage;
			int start = dataPage.getPageNumber() - block;
			if (start < 1)
				start = 1;
			if (end > block * 2)
				start = end - block * 2 + 1;
			for (int i = start; i <= end; i++) {
				if (i != dataPage.getPageNumber()) {
					pageBar.append("\n&nbsp<a href='").append(actionUrl);
					pageBar.append("&page=").append(i).append("'>");
					pageBar.append(i).append("</a>");
				} else {
					if (allPage > 1)
						pageBar.append("&nbsp&nbsp<span class='currPageNumber'>").append(i).append(
								"</span>");
				}
			}
			if (dataPage.getPageNumber() < allPage) {
				pageBar.append("&nbsp;<a href='").append(actionUrl);
				pageBar.append("&page=").append(dataPage.getPageNumber() + 1).append("'>");
				pageBar.append("下一页</a>");
			}
			pageBar.append("</td></tr></table>");
			return pageBar.toString();
		} catch (Exception e) {
			System.out.print("\n\n\n\nee=" + e);
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 产生产品类别下拉菜单的html源码
	 * @param maincatproduct 产品类别数据
	 * @param selectName 下拉菜单名
	 * @param disableSelectParent  是否禁止选择父级菜单
	 * @return
	 */
	public static String getCatProductSelect(List maincatproduct, String selectName, boolean disableSelectParent) {
		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		if (maincatproduct != null)
			for (int i = 0; i < maincatproduct.size(); i++) {
				Dict dict = (Dict) maincatproduct.get(i);
				String dictName = dict.getName();
				dictName = dictName.replaceAll("'", "`");
				if (dict.getParent() == null) {
					if (sb1.length() > 1)
						sb1.append(",");
					sb1.append("\n  new SubSelectObj('").append(dict.getId()).append("','");
					sb1.append("").append("','");
					sb1.append(dictName).append("')");
				} else {
					if (sb2.length() > 1)
						sb2.append(",");
					sb2.append("\n  new SubSelectObj('").append(dict.getId()).append("','");
					sb2.append(dict.getParent().getId()).append("','");
					sb2.append(dictName).append("')");
				}
			}
		String selectId = UUID.create("select");
		StringBuffer content = new StringBuffer();
		content.append("  <select name=\"");
		content.append(selectName);
		content.append("\" id=\"");
		content.append(selectId);
		content.append("\"  class=\"input01\" ");
		if (disableSelectParent)
			content.append("       onchange=\"checkCascadSelect(this)\"  ");
		content.append("    > ");
		content.append("     <option value=''>-- 选择产品类别 --</option>");
		content.append("  </select>");
		content.append("  <script src=\"/script/kylin.js\"></script>");
		content.append("  <script>");
		content.append("      \n var "+selectName+"ParentArray = new Array(" + sb1 + ");  ");
		content.append("      \n var "+selectName+"SubArray = new Array(" + sb2 + ");     ");
		content.append("      \n var theCatSelect =  document.getElementById(\""+selectId+"\");     ");
		content.append("      \n setCascadSelect("+selectName+"ParentArray,"+selectName+"SubArray,theCatSelect);  ");
		content.append("   </script>  ");
		return content.toString();
	}

	/**
	 * 根据 文件类型返回i对应的多媒体文件播放html源码
	 * @param fileType
	 * @param fileUrl
	 * @param width_height
	 * @return
	 */
	public static String getMediaPlay(String fileType ,String fileUrl,String width_height ){
		if (fileType != null && fileUrl != null) {
			String ext = fileUrl.substring(fileUrl.lastIndexOf(".") + 1,fileUrl.length());
			StringBuffer sb = new StringBuffer();
			if ("swf".equalsIgnoreCase(ext)) {
				sb.append("		<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'                                          ");    
				sb.append("		   codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'    ");
				sb.append("		    ").append(width_height).append(">");   
				sb.append("		  <param name='movie' value='/fileOutServlet?fileType="+Constants.MEDIA_VOCABUL+"&fileUrl="+fileUrl+"'>");
				sb.append("		  <param name='quality' value='high'>                            ");
				sb.append("		  <embed src=' ' quality='high'                                  ");
				sb.append("		  pluginspage='http://www.macromedia.com/go/getflashplayer'      ");
				sb.append("		  type='application/x-shockwave-flash' width='227' height='187'> ");
				sb.append("		  </embed>  ");
				sb.append("		</object>   ");

			}else if ("wma".equalsIgnoreCase(ext) || "mp3".equalsIgnoreCase(ext)){
				sb.append("			<embed src='/fileOutServlet?fileType=<%=Constants.MEDIA_VOCABUL%>&fileUrl=<%=fileUrl%>'"); 
				sb.append("			  hidden='false' autostart ='true'   ");
				sb.append("			  id = 'sd1' mastersound>            ");
				sb.append("			</embed> <br>                        ");
				
				sb.append("			<object id='kylinMPlay' name='kylinMPlay'    ");                              
				sb.append("			    type='application/x-oleobject'   ");
				sb.append("			    width='320' height='310'         ");
				sb.append("			    standby='Loading Microsoft Windows Meda Player components...' ");
				sb.append("			    align='center' bgcolor='Black'                                ");
				sb.append("			    classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'          ");
				sb.append("			    codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'>  ");
				sb.append("				<embed src='http://vod.alibaba.com/50/01/88/06/50018806_56.asf' ");
				sb.append("				    type='application/x-mplayer2'                               ");
				sb.append("				    pluginspage='http://www.microsoft.com/windows95/downloads/contents/wurecommended/s_wufeatured/mediaplayer/default.asp' ");
				sb.append("				    width='320' height='308' ");
				sb.append("				    name='kylinMPlay'            ");
				sb.append("				    showcontrols='1'         ");
				sb.append("				    showdisplay='0'          ");
				sb.append("				    showstatusbar='1'>       ");
				sb.append("				</embed>                     ");
				sb.append("			</object>                      ");
				sb.append("			  如果不能正常播放，请将 "+ext+" 文件的打开放式关联到 windows media play ");
				
			}else{
				sb.append("未知的媒体文件类型: "+ext);
			}
			return sb.toString();
	}else
		return null;
		
	}
	
	/**
	 * FCK 的语言种类
	 * @param language
	 * @return
	 */
	public static String getFCKLang(String language){
		String FCKLang = "zh_CN";
		if(language != null){
			if(!language.equals(FCKLang) && language.indexOf("_") > 0){
				FCKLang = language.substring(0,language.indexOf("_"));
			}else
				FCKLang = language;
		}
		return FCKLang;
	}
	
	public static Object checkNull(Object obj) {
		if (obj == null)
			return "";
		else
			return obj;
	}

	/**
	 * 将日期对象转化为yyyy-MM-dd 显示格式的字符串
	 * @param theDate
	 * @return
	 */
	public static String getShowDate(Date theDate){
		if(theDate == null)
			return "";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(theDate);
	}

	/**
	 * 将日期对象转化为yyyy-MM-dd HH:mm:ss 显示格式的字符串
	 * @param theDate
	 * @return
	 */
	public static String getShowDateTime(Date theDate){
		if(theDate == null)
			return "";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(theDate);
	}

	 /**
     * 取字符串的前toCount个字符
     *
     * @param str 被处理字符串
     * @param toCount 截取长度
     * @param more 后缀字符串
     * @version 2008.02.14
     * @return String
     */
    public static String substring(String str, int toCount,String more)
    {
      int reInt = 0;
      String reStr = "";
      if (str == null)
        return "";
      char[] tempChar = str.toCharArray();
      for (int i = 0; (i < tempChar.length && toCount > reInt); i++) {
        String s1 = str.valueOf(tempChar[i]);
        byte[] b = s1.getBytes();
        reInt += b.length;
        reStr += tempChar[i];
      }
      if (toCount == reInt || (toCount == reInt - 1))
        reStr += more;
      return reStr;
    }
	 /**
     * 取字符串的前len个字符
     *
     * @param str 被处理字符串
     * @param len 截取长度
     * @version 2008.02.14
     * @return String
     */
	public static String strcut(String str,int len) {
		
		if (str.length() > len) {
			String resultstr;
			
			resultstr = str.substring(0, len) + "...";
			return resultstr;
		} else {
			return str;
		}
	}

}
